package com.gp.geits.service.impl;

import com.gp.geits.Enum.ResultCodeEnum;
import com.gp.geits.entity.IssueEntity;
import com.gp.geits.entity.QuestionnaireEntity;
import com.gp.geits.entity.UserEntity;
import com.gp.geits.exception.GETSException;
import com.gp.geits.service.IssueService;
import com.gp.geits.service.QuestionnaireService;
import com.gp.geits.service.UserService;
import com.gp.geits.utils.JwtInfo;
import com.gp.geits.utils.PageUtils;
import com.gp.geits.utils.Query;
import com.gp.geits.vo.AnswerSubmitVo;
import com.gp.geits.vo.IssueAndAnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gp.geits.dao.ResponseDao;
import com.gp.geits.entity.ResponseEntity;
import com.gp.geits.service.ResponseService;

import javax.servlet.http.HttpServletRequest;


@Service("responseService")
public class ResponseServiceImpl extends ServiceImpl<ResponseDao, ResponseEntity> implements ResponseService {

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
    private IssueService issueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ResponseEntity> page = this.page(
                new Query<ResponseEntity>().getPage(params),
                new QueryWrapper<ResponseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void answerSubmit(List<AnswerSubmitVo> submitVoList, HttpServletRequest request) {
        //登录检查
        JwtInfo jwtInfo = userService.loginCheck(request);
        Long no = jwtInfo.getNo();

        UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", no)
                .or()
                .eq("stu_no", no));

        Integer id = questionnaireService.getOne(new QueryWrapper<QuestionnaireEntity>()
                .eq("name", submitVoList.get(0).getQuestionnaire()))
                .getId();
        if (id == null){
            throw new GETSException(ResultCodeEnum.QUESTIONNAIRE_NOT_EXISTS);
        }

        List<ResponseEntity> responseEntities = new ArrayList<>();
        ResponseEntity responseEntity;

        for (AnswerSubmitVo vo : submitVoList){
            responseEntity = new ResponseEntity();

            Integer issueId = issueService.getOne(new QueryWrapper<IssueEntity>()
                    .eq("issue", vo.getIssue())
                    .eq("questionnaire_id", id)).getId();

            if (issueId == null){
                throw new GETSException(ResultCodeEnum.QUESTION_NOT_EXISTS);
            }

            responseEntity.setQuestionnaireId(id);
            responseEntity.setIssueId(issueId);
            responseEntity.setUserId(userEntity.getId());
            responseEntity.setAnswer(vo.getAnswer());

            responseEntities.add(responseEntity);
        }

        //批量保存
        saveBatch(responseEntities);
    }

    @Override
    public List<String> checkQuestionnaireByUserId(HttpServletRequest request) {
        Long no = userService.loginCheck(request).getNo();

        Integer userId = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", no)
                .or()
                .eq("stu_no", no)).getId();


        List<ResponseEntity> responseEntityList = baseMapper.selectList(new QueryWrapper<ResponseEntity>()
                .eq("user_id", userId));

        if (responseEntityList.size() < 1){
            throw new GETSException(ResultCodeEnum.USER_RESPONSE_NOT_EXISTS);
        }

        List<String> questionnaireList = new ArrayList<>();
        String name;

        for (ResponseEntity vo : responseEntityList){
            Integer questionnaireId = vo.getQuestionnaireId();

            name = questionnaireService.getOne(new QueryWrapper<QuestionnaireEntity>()
                    .eq("id", questionnaireId)).getName();

            questionnaireList.add(name);
        }

        return questionnaireList
                .stream().distinct()//去重
                .collect(Collectors.toList());
    }

    @Override
    public IssueAndAnswerVo issueAndAnswerByQuestionnaireName(String name, HttpServletRequest request) {
        Long no = userService.loginCheck(request).getNo();

        Integer userId = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", no)
                .or()
                .eq("stu_no", no)).getId();

        QuestionnaireEntity questionnaireEntity = questionnaireService.getOne(new QueryWrapper<QuestionnaireEntity>()
                .eq("name", name));

        List<ResponseEntity> responseEntityList = baseMapper.selectList(new QueryWrapper<ResponseEntity>()
                .eq("user_id", userId)
                .eq("questionnaire_id", questionnaireEntity.getId()));

        IssueAndAnswerVo vo = new IssueAndAnswerVo();
        vo.setQuestionnaire(name);

        List<String> issueList = new ArrayList<>();
        List<String> answerList = new ArrayList<>();

        for (ResponseEntity entity : responseEntityList){
            issueList.add(issueService.getOne(new QueryWrapper<IssueEntity>()
                    .eq("id",entity.getIssueId()))
                    .getIssue());

            answerList.add(entity.getAnswer());
        }

        vo.setIssueList(issueList);
        vo.setAnswerList(answerList);

        return vo;
    }

    @Override
    public List<IssueAndAnswerVo> issueAndAnswerList() {
        List<IssueAndAnswerVo> issueAndAnswerVoList = new ArrayList<>();
        IssueAndAnswerVo issueAndAnswerVo;
        List<String> issueList;
        List<String> answerList;

        List<Integer> groupByQuestionnaireIds = baseMapper.selectGroupByQuestionnaireId();

        for (Integer questionnaireId : groupByQuestionnaireIds){
            issueAndAnswerVo = new IssueAndAnswerVo();
            issueList = new ArrayList<>();
            answerList = new ArrayList<>();

            String questionnaireName = questionnaireService.getOne(new QueryWrapper<QuestionnaireEntity>()
                    .eq("id", questionnaireId))
                    .getName();
            issueAndAnswerVo.setQuestionnaire(questionnaireName);

            List<IssueEntity> issueEntities = issueService.getQuestionnaireByName(questionnaireId);

            for (IssueEntity issueEntity : issueEntities){
                issueList.add(issueEntity.getIssue());
            }
            issueAndAnswerVo.setIssueList(issueList);

            List<ResponseEntity> responseEntities = baseMapper.selectList(new QueryWrapper<ResponseEntity>()
                    .eq("questionnaire_id", questionnaireId));

            for (ResponseEntity responseEntity1 : responseEntities){
                answerList.add(responseEntity1.getAnswer());
            }
            issueAndAnswerVo.setAnswerList(answerList);

            issueAndAnswerVoList.add(issueAndAnswerVo);
        }

        return issueAndAnswerVoList;
    }

}